<?php

namespace App\Services\Index;

use App\Services\Service;
use App\Services\Traits\DatabaseMysql;
use App\Models\Game\Settlement\Info as SettlementInfo;
use App\Models\Game\Stats\User\Day;
use App\Models\Game\User\User;
use App\Core\Traits\Numeral;

class SummaryService extends Service
{
    use DatabaseMysql, Numeral;

    /**
     * 概要统计【注册人数】
     */
    public function playerRegister($request)
    {
        $query = User::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);

        $querySum = (clone $query);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $querySum, 'registration_time');
        // 数据合计
        $total = $querySum->count();

        // 筛选【时间】最近7天
        $this->makeWhereLastDays($request, $query, 'registration_time', 7);
        // 统计数据【数据列表】
        $collection = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(registration_time, '%Y%m%d')) AS timed",
            "COUNT(*) AS amount"
        ]))
            ->groupBy('timed')
            ->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 概要统计【游戏人数】
     */
    public function playerGame($request)
    {
        $query = SettlementInfo::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);
        // 筛选【类型】
        $this->makeWhere($query, 'player_type', SettlementInfo::PLAYER_TYPE);

        $querySum = (clone $query);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $querySum, 'end_time');
        // 数据合计
        $total = $querySum->distinct('uid')->count();

        // 筛选【时间】最近7天
        $this->makeWhereLastDays($request, $query, 'end_time', 7);
        // 统计数据【数据列表】
        $collection = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(end_time, '%Y%m%d')) AS timed",
            "COUNT(DISTINCT uid) AS amount"
        ]))
            ->groupBy('timed')
            ->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 概要统计【新增游戏人数】
     */
    public function playerGameNew($request)
    {
        $query = User::dbTable();
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);

        $querySum = (clone $query);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $querySum, 'first_game_time');
        // 数据合计
        $total = $querySum->count();

        // 筛选【时间】最近7天
        $this->makeWhereLastDays($request, $query, 'first_game_time', 7);
        // 统计数据【数据列表】
        $collection = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(first_game_time, '%Y%m%d')) AS timed",
            "COUNT(*) AS amount"
        ]))
            ->groupBy('timed')
            ->get();

        return [
            $collection,
            $total
        ];
    }

    /**
     * 概要统计【平台收益】
     */
    public function amount($request)
    {
        $query = SettlementInfo::dbTable();
        // 筛选【类型】
        $this->makeWhere($query, 'player_type', SettlementInfo::PLAYER_TYPE);
        // 筛选【平台】
        $this->makeWhereOrg($request, $query);

        $querySum = (clone $query);
        // 筛选【时间】
        $this->makeWhereParamDatetime($request, $querySum, 'end_time');
        // 数据合计
        $total = -$querySum->sum('usd->gain_player');

        // 筛选【时间】最近7天
        $this->makeWhereLastDays($request, $query, 'end_time', 7);
        // 统计数据【数据列表】
        $collection = $query->selectRaw(implode(',', [
            "UNIX_TIMESTAMP(FROM_UNIXTIME(end_time, '%Y%m%d')) AS timed",
            "CAST(-SUM(usd->'$.gain_player') AS DECIMAL({$this->decimals()},{$this->precision()})) AS amount"
        ]))
            ->groupBy('timed')
            ->get();

        return [
            $collection,
            $total
        ];
    }
}
